route.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ResultDto } from '@/types/response/common';
  3. import { LoginResponse } from '@/types/response/auth';
  4. import { fetchJson } from '@/lib/utils/server';
  5. export async function GET(request: NextRequest, { params }: { params: Promise<{ path: string[] }> })
  6. {
  7. const { path } = await params;
  8. const searchParams = request.nextUrl.searchParams.toString();
  9. const endpoint = `/api/auth/${path.join('/')}${searchParams ? `?${searchParams}` : ''}`;
  10. const res: ResultDto = await fetchJson(endpoint, {
  11. method: 'GET'
  12. });
  13. return NextResponse.json(res);
  14. }
  15. export async function POST(request: NextRequest, { params }: { params: Promise<{ path: string[] }> })
  16. {
  17. const { path } = await params;
  18. const endpoint = `/api/auth/${path.join('/')}`;
  19. const body = request.headers.get('content-type')?.includes('application/json') ? await request.json() : null;
  20. const res: ResultDto = await fetchJson(endpoint, {
  21. method: 'POST',
  22. ...(body && { body: JSON.stringify(body) })
  23. });
  24. const response = NextResponse.json(res);
  25. // 로그인 또는 토큰 갱신 성공 시 쿠키 설정
  26. if ((path[0] === 'login' || path[0] === 'google-login' || path[0] === 'refresh-token') && res.success && res.data) {
  27. const data = res.data as LoginResponse;
  28. const cookieOptions = { httpOnly: true, path: '/' };
  29. response.cookies.set('accessToken', data.accessToken, cookieOptions);
  30. response.cookies.set('refreshToken', data.refreshToken, cookieOptions);
  31. }
  32. if (path[0] === 'logout' && res.success) {
  33. response.cookies.delete('accessToken');
  34. response.cookies.delete('refreshToken');
  35. }
  36. return response;
  37. }